/*	This file is part of LSAC.

	LSAC is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	LSAC is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with LSAC.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (c) 2010 LEPESME "Jiboo" Jean-Baptiste
*/

#include "cElse.h"
#include "lsac.h"
#include "cIf.h"

#include <typeinfo>

cElse::cElse(cLocation pLocation, cBlock *pBlock)
	: cBlock(pLocation, pBlock)
{
}

void cElse::print(int pLevel)
{
	PRINT_LOG("cElse " << this);
	cBlock::print_statements(pLevel);
}

cElse::~cElse()
{
}

void cElse::validate(cSemContext *pContext)
{
	// Faut que le statement precedant dans le pere soit un if

	bool lFound = false;

	std::list<cStatement*>::iterator lIterator = this->dParent->dStatements.begin();
	for(; lIterator != this->dParent->dStatements.end(); lIterator++)
	{
		if((*lIterator) == this)
		{
			lIterator--;
			if(typeid(*(*lIterator)) == typeid(cIf))
			{
				lFound = true;
			}
			break;
		}
	}

	if(!lFound) ERROR("Erreur en " << this->dLocation << " : un block 'sinon' doit etre precede d' un block if.");

	this->validate_statements(pContext);
	pContext->removeLocals(this);
}

void cElse::genCode(ostream &pStream)
{
	pStream << "else" << endl << "{" << endl;
	this->gen_statements(pStream);
	pStream << "}" << endl;
}
